﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;
using MvcEngine.Gallery.Managers;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.Extensions;

namespace MvcEngine.Gallery.Components.Extensions
{
    public static class UrlHelperExtensions
    {
        #region Gallery

        public static string Gallery(this UrlHelper urlHelper)
        {
            return urlHelper.Page();
        }

        public static string Gallery(this UrlHelper urlHelper, string galleryName)
        {
            return urlHelper.RouteUrl("BackOffice_Gallery", new { galleryName = galleryName });
        }

        public static string Gallery(this UrlHelper urlHelper, Guid galleryId)
        {
            var gallery = GalleryManager.Current.GetGallery(galleryId);
            return urlHelper.Gallery(gallery.Name);
        }

        public static string Galleries(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryList");
        }

        public static string GalleryInstall(this UrlHelper urlHelper, Guid pageId)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryInstall", new { pageId = pageId });
        }

        public static string GallerySettings(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GallerySettings");
        }

        public static string GallerySettingsSave(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GallerySettingsSave");
        }

        #endregion

        #region Image

        public static string ImagePage(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryImagePage", new { pageNumber = pageNumber });
        }

        public static string DeleteImage(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryDeleteImage", new { id = id });
        }

        public static string EditImage(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryEditImage", new { id = id });
        }

        public static string NewImage(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryNewImage");
        }

        public static string GalleryImage(this UrlHelper urlHelper, string pageName, string fileName)
        {
            return urlHelper.RouteUrl("StarterSite_GalleryImageFile", new { pageName = pageName, fileName = fileName });
        }

        #endregion

        #region Category

        public static string GalleryCategories(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryCategoryList");
        }

        public static string GalleryCategories(this UrlHelper urlHelper, int pageNumber)
        {
            return urlHelper.RouteUrl("BackOffice_BlogCategoryPaging", new { pageNumber = pageNumber });
        }

        public static string EditCategory(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryEditCategory", new { id = id });
        }

        public static string NewCategory(this UrlHelper urlHelper)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryNewCategory");
        }

        public static string DeleteCategory(this UrlHelper urlHelper, Guid id)
        {
            return urlHelper.RouteUrl("BackOffice_GalleryDeleteCategory", new { id = id });
        }

        public static string CategoryFilterPage(this UrlHelper urlHelper, int pageNumber)
        {
            string categoryName = (string)urlHelper.RequestContext.RouteData.Values["categoryName"];
            if (string.IsNullOrEmpty(categoryName))
            { 
                var gallery = GalleryManager.Current.GetCurrentGallery();
                if (gallery.DefaultCategoryId.HasValue)
                {
                    var category = GalleryCategoryManager.Current.Get(gallery.DefaultCategoryId.Value);
                    if (category != null)
                    {
                        categoryName = category.Slug;
                    }
                }
            }
            return urlHelper.RouteUrl("GalleryCategoryFilterPage", new { categoryName = categoryName, pageNumber = pageNumber });
        }

        public static string CategoryFilter(this UrlHelper urlHelper, Guid galleryId, string categoryName)
        {
            var gallery = GalleryManager.Current.GetGallery(galleryId);
            var page = ContentManager.Current.GetPage(gallery.PageId);
            return urlHelper.RouteUrl("GalleryCategoryFilter", new { pageName = page.Name, categoryName = categoryName });
        }

        public static string CategoryFilter(this UrlHelper urlHelper, string categoryName)
        {
            return urlHelper.RouteUrl("GalleryCategoryFilter", new { categoryName = categoryName });
        }

        #endregion
    }
}
